package cn.edu.neusoft.maxingxin231.yuejian06.Activity;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.Club.ClubFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.HomeFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.MyFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.PostFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.Fragment.TrainFragment;
import cn.edu.neusoft.maxingxin231.yuejian06.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
        TrainFragment.OnFragmentInteractionListener,
        ClubFragment.OnFragmentInteractionListener,
        HomeFragment.OnFragmentInteractionListener,
        PostFragment.OnFragmentInteractionListener,
        MyFragment.OnFragmentInteractionListener {
    LinearLayout tab_train;
    LinearLayout tab_club;
    LinearLayout tab_home;
    LinearLayout tab_share;
    LinearLayout tab_me;
    ImageView img_train1;
    ImageView img_club1;
    ImageView img_home1;
    ImageView img_share1;
    ImageView img_me1;
    Fragment fragment_train;
    Fragment fragment_club;
    Fragment fragment_home;
    Fragment fragment_post;
    Fragment fragment_my;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(0);

    }

    private void initView() {
        tab_train = (LinearLayout)findViewById(R.id.tab_train);
        tab_club = (LinearLayout)findViewById(R.id.tab_club);
        tab_home= (LinearLayout)findViewById(R.id.tab_home);
        tab_share = (LinearLayout)findViewById(R.id.tab_share);
        tab_me = (LinearLayout)findViewById(R.id.tab_me);

        img_club1 = (ImageView)findViewById(R.id.img_club1);
        img_train1 =(ImageView)findViewById(R.id.img_train1);
        img_home1 =(ImageView)findViewById(R.id.img_home1);
        img_share1 =(ImageView)findViewById(R.id.img_share1);
        img_me1 =(ImageView)findViewById(R.id.img_me1);
    }

    private void initEvent() {
        tab_train.setOnClickListener(this);
        tab_club.setOnClickListener(this);
        tab_home.setOnClickListener(this);
        tab_share.setOnClickListener(this);
        tab_me.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //将所有图片置成normal的那张
        resetImage();
        //选哪个，哪个fragment显示出来
        //同时图片置成press那张
        switch (view.getId()){
            case R.id.tab_train:
                setSelect(0);
                break;
            case R.id.tab_club:
                setSelect(1);
                break;
            case R.id.tab_home:
                setSelect(2);
                break;
            case R.id.tab_share:
                setSelect(3);
                break;
            case R.id.tab_me:
                setSelect(4);
                break;
             default:
                 break;
        }

    }

    private void resetImage() {
        img_train1.setImageResource(R.drawable.ic_train1);
        img_club1.setImageResource(R.drawable.ic_club1);
        img_home1.setImageResource(R.drawable.ic_home1);
        img_share1.setImageResource(R.drawable.ic_share1);
        img_me1.setImageResource(R.drawable.ic_me1);
    }

    private void setSelect(int i) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i){
            case 0:
                if (fragment_train == null){
                    fragment_train = new TrainFragment();
                    fragmentTransaction.add(R.id.content,fragment_train);
                }
                else {
                    fragmentTransaction.show(fragment_train);
                }
                img_train1.setImageResource(R.drawable.ic_train);
                break;
            case 1:
                if (fragment_club == null){
                    fragment_club = new ClubFragment();
                    fragmentTransaction.add(R.id.content,fragment_club);
                }
                else {
                    fragmentTransaction.show(fragment_club);
                }
                img_club1.setImageResource(R.drawable.ic_club);
                break;
            case 2:
                if (fragment_home == null){
                    fragment_home = new HomeFragment();
                    fragmentTransaction.add(R.id.content,fragment_home);
                }
                else {
                    fragmentTransaction.show(fragment_home);
                }
                img_home1.setImageResource(R.drawable.ic_home);
                break;
            case 3:
                if (fragment_post == null){
                    fragment_post = new PostFragment();
                    fragmentTransaction.add(R.id.content,fragment_post);
                }
                else {
                    fragmentTransaction.show(fragment_post);
                }
                img_share1.setImageResource(R.drawable.ic_share);
                break;
            case 4:
                if (fragment_my == null){
                    fragment_my = new MyFragment();
                    fragmentTransaction.add(R.id.content,fragment_my);
                }
                else {
                    fragmentTransaction.show(fragment_my);
                }
                img_me1.setImageResource(R.drawable.ic_me);
                break;
             default:
                 break;
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_train != null){
            fragmentTransaction.hide(fragment_train);
        }
        if (fragment_club != null){
            fragmentTransaction.hide(fragment_club);
        }
        if (fragment_home != null){
            fragmentTransaction.hide(fragment_home);
        }
        if (fragment_post != null){
            fragmentTransaction.hide(fragment_post);
        }
        if (fragment_my != null){
            fragmentTransaction.hide(fragment_my);
        }

    }



    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
